/**
 * Created by stillyuk on 16/5/27.
 */

angular.module('app.core', [])
    .provider('iHttp', function () {
        var local = true;
        this.localData = function () {
            local = true;
        };

        this.$get = ['$http', 'mockData', '$q', '$timeout', function ($http, mockData, $q, $timeout) {
            return {
                get: function (url, option) {
                    var defer = $q.defer();
                    if (local) {
                        $timeout(function () {
                            var result = mockData.get(url, option);
                            if (result) {
                                defer.resolve(result);
                            } else {
                                $http.get(url).then(function (result) {
                                    defer.resolve(result)
                                });
                            }
                        }, 100);
                    }

                    return defer.promise;
                }
            }
        }];
    });